import {
  wxRequest
} from '../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()
const moment = require('../../utils/moment')
Page({

  data: {
    imgUrl: app.globalData.imgUrl,
    uploadUrl: wx.getStorageSync('uploadUrl'),
    pageNo: 1,
    hasNextPage: false,
    list: [],
    keyword: '',
    tabId: 1,
    odrData: {},
    statusList: [{
        id: '',
        text: "全部"
      }, {
        id: 1,
        text: "待付款"
      }, {
        id: 2,
        text: "待收货"
      }, {
        id: 3,
        text: "已完成"
      }, {
        id: 4,
        text: "已取消"
      }, {
        id: 6,
        text: "已退款"
      },
      {
        id: 5,
        text: "线下订单"
      },
      {
        id: 7,
        text: "线上订单"
      }
    ],
    status: '',
    currentDate: '',
    startDate: '',
    orgList: [
      { id: 4, value: '供应商' }
    ],
    orgTypeName: '供应商',
    orgType: '',
    org: wx.getStorageSync('userInfo').orgType,
  },

  // toDtail:function(){
  //   wx:wx.navigateTo({
  //     url: '/pages/menber/mall/detail'
  //   })
  // },

  onLoad: function (options) {
    if (options.name) {
      this.data.keyword = options.name
      this.setData({
        keyword: this.data.keyword,
      })
    }
    // this.initDate();
    if (this.data.org == 1) {
      this.getSupplierList();
    } else {
      this.getlist();
    }
  },

  initDate() {
    let date = moment().format('YYYY-MM-DD')
    let startDate = moment('2020-01-01').format('YYYY-MM-DD')
    this.setData({
      startDate: startDate,
      currentDate: date
    })
  },

  bindStartDate(e) {
    if (moment(e.detail.value).unix() > moment(this.data.currentDate).unix()) {
      wx.showModal({
        icon: 'error',
        title: '起始日期不能超过结束日期'
      })
      return
    }
    if (moment(e.detail.value).unix() > moment().unix()) {
      wx.showModal({
        icon: 'error',
        title: '所选日期不能超过当前日期'
      })
      return
    }
    this.setData({
      startDate: e.detail.value
    })
  },

  bindCurrentDate(e) {
    if (moment(e.detail.value).unix() < moment(this.data.startDate).unix()) {
      wx.showModal({
        icon: 'error',
        title: '结束日期不能小于初始日期'
      })
      return
    }
    if (moment(e.detail.value).unix() > moment().unix()) {
      wx.showModal({
        icon: 'error',
        title: '所选日期不能超过当前日期'
      })
      return
    }
    this.setData({
      currentDate: e.detail.value
    })
  },

  //选择状态
  checkOrg: function(e) {
    var orgType = e.currentTarget.dataset.id
    this.setData({
      orgType
    })
  },

  onShow: function () {},

  //tab切换
  handleTab: function (e) {
    var tabId = e.currentTarget.dataset.id
    this.setData({
      tabId
    })
    this.getlist()
  },

  //查询列表
  getlist: function () {

    var url = '/odr/protected/order/findAll'
    // if (this.data.tabId == 2) {
    //   url = '/odr/protected/orgOrder/findAll'
    // } 
    let params = {
      keyword: this.data.keyword,
      pageNo: this.data.pageNo || 1,
      overPay: 1,
      startDate: this.data.startDate,
      endDate: this.data.currentDate,
    }
    // 如果是平台才给筛选供应商
    if (this.data.org == 1 && this.data.orgType != '') {
      params.orgType = this.data.orgType
    }
    if (this.data.status == 6) {
      params.cancel = 1
    } else if (this.data.status == 5) {
      params.odrType = 3
      params.status = ''
    } else if (this.data.status == 7) {
      params.odrType = 2
      params.status = ''
    } else {
      params.status = this.data.status
    }
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + url,
      method: 'POST',
      data: params,
    }, (res) => {
      let list = res.list;
      if (this.data.pageNo !== 1) {
        list = this.data.list.concat(list);
      }

      list.map(item => {
        var jsondata = JSON.parse(item.detailJson);
        var cancelflag = jsondata.some(item => {
          item.cancel == 1
        })
        if (cancelflag) {
          item.cancelData = 1;
          console.log('1111')
        }
      })

      if (this.data.keyword == '') {
        this.data.odrData = res.queryStatistic
      } else {
        this.data.odrData = res.odrData
        this.data.odrData.odrAmt = res.statisticData
        this.data.odrData.odrQty = res.page.totalSize
      }

      this.data.queryStatistic = {
        totalOdrAmount: res.statisticData,
        totalOdrCount: res.page.totalSize
      }
      this.setData({
        list: list,
        odrData: this.data.odrData,
        queryStatistic: this.data.queryStatistic,
        pageNo: res.page.pageNo,
        hasNextPage: res.page.hasNextPage,
      });

    })
  },

  handleOfflineOdr: function () {

  },

  //下拉刷新
  onPullDownRefresh: function () {
    this.refreshData();
    wx.stopPullDownRefresh()
  },

  refreshData: function () {
    this.setData({
      pageNo: 1
    })
    this.getlist();
  },

  //触底时，加载更多
  onReachBottom: function () {
    if (this.data.hasNextPage) {
      this.setData({
        pageNo: this.data.pageNo + 1
      });
      this.getlist();
    }
  },

  //搜索关键词
  seachInputConfirm: function (e) {
    this.setData({
      pageNo: 1,
      keyword: e.detail.value
    })
    this.getlist();
  },

  seachBtn: function () {
    this.setData({
      pageNo: 1,
      keyword: this.data.keyword
    })
    this.getlist();
  },

  seachInputVal: function (e) {
    var inputval = e.detail.value;
    console.log(e)
    this.setData({
      keyword: inputval
    })
  },

  //清空
  clearInput: function () {
    this.setData({
      keyword: ''
    })
    this.getlist();
  },

  //********************筛选更多********************
  popupRight: function () {
    this.setData({
      showPopup: true
    })
  },

  popupCancel: function () {
    this.setData({
      showPopup: false
    })
  },

  popupConfirm: function () {
    this.getlist();
    this.popupCancel();
  },

  //选择状态
  checkStatus: function (e) {
    var status = e.currentTarget.dataset.id || ''
    console.log(status, 'status')
    this.setData({
      status
    })
  },
  //********************筛选更多end********************





  toDtail: function (e) {
    var id = e.currentTarget.dataset.id;
    console.log(id, 'id')
    wx.navigateTo({
      url: '/pages/menber/mall/detail?id=' + e.currentTarget.dataset.id + "&typefg=1" + "&type=" + this.data.tabId,
    })
  }











})